
package com.amigo.online.provider.manager.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.amigo.online.common.base.service.BaseService;
import com.amigo.online.provider.manager.mapper.PromoteOrderMapper;
import com.amigo.online.provider.manager.mapper.VideoPromoteMapper;
import com.amigo.online.provider.manager.model.entity.PromoteOrderTbl;
import com.amigo.online.provider.manager.model.entity.VideoPromoteTbl;
import com.amigo.online.provider.manager.result.OrderInfo;
import com.amigo.online.provider.manager.service.OrderService;

/**
 * <code>OrderServiceImpl.java</code>
 * <p>
 *
 * </p>
 * <p>
 *
 * @author LuChen
 * @version 1.0
 * @company Amigo
 * @time 2018年12月3日 上午11:02:11
 * @lastUpdate 无
 * </p>
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class OrderServiceImpl extends BaseService<PromoteOrderTbl> implements OrderService {


    @Autowired
    private PromoteOrderMapper promoteOrderMapper;

    @Autowired
    private VideoPromoteMapper videoPromoteMapper;


    @Override
    public JSONObject getOrderList(PromoteOrderTbl order) {
        if (order.getPageNo() == null || order.getPageNo() <= 0) {
            order.setPageNo(1);
        }
        if (order.getPageCount() == null || order.getPageCount() <= 0) {
            order.setPageCount(10);
        }
        Integer totalCount = promoteOrderMapper.getOrderListTotalCount(order);
        Integer totalPage = totalCount % order.getPageCount() == 0 ? totalCount / order.getPageCount() : totalCount / order.getPageCount() + 1;
        Integer start = (order.getPageNo() - 1) * order.getPageCount();
        order.setPageNo(start);
        List<PromoteOrderTbl> list = promoteOrderMapper.getOrderList(order);
        JSONObject re = new JSONObject();
        re.put("list", list);
        re.put("pageNo", order.getPageNo());
        re.put("pageCount", order.getPageCount());
        re.put("totalCount", totalCount);
        re.put("totalPage", totalPage);
        return re;
    }


    @Override
    public OrderInfo getOrderInfo(Long id) {
        return videoPromoteMapper.getOrderInfo(id);
    }


    @Override
    public JSONObject getPromoteList(VideoPromoteTbl videoPromoteTbl) {
        if (videoPromoteTbl.getPageNo() == null || videoPromoteTbl.getPageNo() <= 0) {
            videoPromoteTbl.setPageNo(1);
        }
        if (videoPromoteTbl.getPageCount() == null || videoPromoteTbl.getPageCount() <= 0) {
            videoPromoteTbl.setPageCount(10);
        }
        Integer totalCount = videoPromoteMapper.getVideoPromoteTotalCount(videoPromoteTbl);
        Integer pageCount = totalCount % videoPromoteTbl.getPageCount() == 0 ? totalCount / videoPromoteTbl.getPageCount() : totalCount / videoPromoteTbl.getPageCount() + 1;
        List<VideoPromoteTbl> list = videoPromoteMapper.getPromoteList(videoPromoteTbl);
        JSONObject re = new JSONObject();
        re.put("list", list);
        re.put("pageNo", videoPromoteTbl.getPageNo());
        re.put("pageCount", videoPromoteTbl.getPageCount());
        re.put("totalCount", totalCount);
        re.put("totalPage", pageCount);
        return re;
    }

}

